#include "buffer.h"

#include <sys/uio.h>

using namespace tiny_muduo;

ssize_t Buffer::ReadFd(int fd)
{
    char extrabuf[65536] = {0};
    struct iovec iv[2];
    const int writable = writablebytes();
    iv[0].iov_base = beginWrite();
    iv[0].iov_len = writable;
    iv[1].iov_base = extrabuf;
    iv[1].iov_len = sizeof(extrabuf);

    const ssize_t readn = ::readv(fd, iv, 2);

    if(readn < 0)
    {
        printf("Buffer::ReadFd readn < 0 SYS_ERR\n");
    }
    else if(readn <= writable)
    {
        write_index_ += readn;
    }
    else
    {
        write_index_ = buffer_.size();
        Append(extrabuf, readn - writable);
    }

    return readn;
}
